package com.econ.powercloud.ems.controller;

import com.econ.powercloud.common.base.facade.pojo.MsgResult;
import com.econ.powercloud.common.base.facade.pojo.PageResult;
import com.econ.powercloud.ems.pojo.DitDataVO;
import com.econ.powercloud.ems.pojo.ExportDataRecordVO;
import com.econ.powercloud.ems.pojo.requestVO.ExportDataRequestVO;
import com.econ.powercloud.ems.pojo.requestVO.HisDataExportRequestVO;
import com.econ.powercloud.ems.service.biz.ExportDataService;
import com.econ.powercloud.ems.util.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @Author : zjf
 * @Description : 数据导出controller
 * @Date : 2023/5/31 11:42
 * @Modified by : zjf
 */
@Api(tags = "数据导出")
@RestController
@RequestMapping(value = "/api/v2/export/data")
public class ExportDataController extends ApiBaseController{

    @Autowired
    private ExportDataService exportDataService;

    @PostMapping("/exportDitHis")
    @ApiOperation(value = "导出点号历史数据")
    public MsgResult exportHisData(@RequestBody @Validated HisDataExportRequestVO<DitDataVO> requestVO) {
        if(requestVO.getEndTime() > System.currentTimeMillis()){
            requestVO.setEndTime(System.currentTimeMillis());
        }
        requestVO.setStartTime(TimeUtil.getDayStartTime(new Date(requestVO.getStartTime())));
        exportDataService.exportHisDitData(requestVO);
        return successHandler(true);
    }

    @ApiOperation(value = "获取导出记录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNo", value = "页码", defaultValue = "1", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页大小", defaultValue = "10", dataType = "int")
    })
    @GetMapping(value = "/recordList")
    public MsgResult<PageResult<List<ExportDataRecordVO>>> getFaultListByCondition(ExportDataRequestVO requestVO,
                                                                                   @RequestParam(defaultValue = "1") int pageNo,
                                                                                   @RequestParam(defaultValue = "10") int pageSize) {
        return successHandler(exportDataService.getRecordList(requestVO, pageNo, pageSize));
    }

    @ApiOperation(value = "下载文件")
    @GetMapping(value = "download/{id}")
    public void downLoadFile(@PathVariable("id") Integer id, HttpServletResponse response){
        exportDataService.downLoadFile(id,response);
    }

    @ApiOperation(value = "删除文件")
    @DeleteMapping(value = "{id}")
    public MsgResult deleteRecord(@PathVariable("id") Integer id){
        exportDataService.deleteRecord(id);
        return successHandler(true);
    }

}
